package com.amkai.aes.webservice.binary.objecthandler.dictionary;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.dictionary.SupplyResourceItemManager;
import com.amkai.aes.aescommon.exceptions.AOException;
import com.amkai.aes.aescommon.service.binary.job.inventory.SupplyResourceItemJob;
import com.amkai.aes.aescommon.service.result.casehistory.SupplyResourceItemResult;
import com.amkai.aes.generatedbeans.cdmcasehistory.logdetails.resources.SupplyLog;
import com.amkai.aes.generatedbeans.inventory.items.SupplyResourceItem;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.server.dictionary.SupplyResourceProviderImpl;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class SupplyResourceItemJobHandler implements JobObjectHandler<SupplyResourceItemJob, Department, SupplyResourceItemResult> {

	private static final String TITLE = "Supply/Resource";
	private static final Log log = LogFactory.getLog(SupplyResourceItemJobHandler.class);
	
	@Override
	public SupplyResourceItemResult process(SupplyResourceItemJob job, Department dep) throws Exception {
		switch (job.getKind()) {
		case GET_COST:	return getCost(job, dep);
		case GET_SUPPLY_RESOURCE_ITEM_BY_ID:	return getSupplyResource(job);
		}
		throw new AOException(TITLE, "Unknown JobKind", job.getKind().toString(), null);
	}
	
	private SupplyResourceItemResult getCost(SupplyResourceItemJob job, Department moduleDepartment) throws Exception {
		SupplyResourceItemResult result = new SupplyResourceItemResult();
		SessionManager sm = SiteManager.Start();
		try {
			// AES-14551
			SupplyResourceProviderImpl srpi = new SupplyResourceProviderImpl(sm);
			if (job.getSupplyResourceItem()!=null)
				result.setCost(srpi.getSupplyResourceCost(sm.get(SupplyResourceItem.class, job.getSupplyResourceItem().getId()), job.getQuantity()));
			// AES-15485, AES-15506
			else if (job.getSupplyLog()!=null)
				result.setCost(srpi.getSupplyResourceCost(sm.get(SupplyLog.class, job.getSupplyLog().getId()), job.getQuantity()));
		}
		catch (Exception ex) {
			log.error(TITLE, ex);
			result.setException(ex);
		}
		finally {
			sm.closeSession();
		}
		return result;
	}
	
	private SupplyResourceItemResult getSupplyResource(SupplyResourceItemJob job) throws Exception {
		SupplyResourceItemResult result = new SupplyResourceItemResult();
		SessionManager sm = SiteManager.Start();
		try {
			SupplyResourceItem sri = sm.get(SupplyResourceItem.class, job.getSupplyResourceItemID());
			SupplyResourceItemManager.loadSupplyResourceItem(sri);
			result.setSupplyResourceItem(sri);
		}
		catch (Exception ex) {
			log.error(TITLE, ex);
			result.setException(ex);
		}
		finally {
			sm.closeSession();
		}
		return result;
	}

/*
	private void loadItem(final Item item) {
		if (item != null) {
			item.getId();
			item.getName();
			item.getShortName();
		}
	}
	
	@Override
	public SupplyResourceItemResult process(SupplyResourceItemJob job, Serializable object) throws Exception {
		SupplyResourceItemResult result = new SupplyResourceItemResult();
		result.setSupplyResourceItem(null);
		
		if (job != null && job.getId() != null && job.getId() > 0) {
			SessionManager sm = SiteManager.Start();
			sm.openSession();
			try {
				SupplyResourceItem item = sm.get(SupplyResourceItem.class, job.getId());
				loadItem(item);
				loadItem(item.getCategory());

				if (item.getInventoryElement() != null && !item.getInventoryElement().isEmpty()) {
					InventoryElement element = item.getInventoryElement().iterator().next(); 
					
					if (element != null && element.getItemLocations() != null) {
						for (ItemLocation location : element.getItemLocations()) {
							location.getId();
							loadItem(location.getLocation());
						}
					}
				}
				
				result.setSupplyResourceItem(item);
			} finally {
				sm.closeSession();
			}
		}
		
		return result;
	}
	*/
}
